<template>
  <!-- <div id="echartsView" class="className"/> -->
  <div>
    <div id="coursetop" style="width:100%; height:500px"></div>
  </div>
</template>

<script>
import echarts from 'echarts'

export default {
  props: {
    courseNameArray: {
      type: Array,
      default() {
        return []
      }
    },
    numArray: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById('coursetop'))
      this.chart.setOption({
        color: ['#3398DB'],
        title: {
          text: '最具价值课程排行榜',
          textStyle: {
            color: '#767676'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: "{b} <br> 热度: {c}%"
        },
        /*legend: {
            data: [date]
        },*/
        grid: {
          left: '4%',
          right: '4%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          boundaryGap: [0, 0.01],
          min: 0,
          max: 100,
          interval: 20,
          axisLabel: {
            formatter: '{value}%',
            textStyle: {
              //color: '#fff',
              fontWeight: '80'
            }
          }
        },
        yAxis: {
          type: 'category',
          data: this.courseNameArray,
          axisLabel: {
            show: true,
            interval: 0,
            rotate: 0,
            margin: 10,
            inside: false,
            textStyle: {
              //color: '#fff',
              fontWeight: '50'
            }
          }
        },
        series: [{
          type: 'bar',
          label: {
            normal: {
              show: true,
              // formatter: '{c}',
              formatter: function (v) {
                var val = v.data;
                if (val == 0) {
                  return '';
                }
                return val;
              },
              color: '#fff'
            }
          },
          data: this.numArray
        }]
      })
    }
  }
}
</script>
